package com.sanctuary.dao.impls;

import java.sql.SQLException;
import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.sanctuary.dao.interfaces.TermDao;
import com.sanctuary.models.Term;

public class TermDaoImpl extends SqlMapClientDaoSupport implements TermDao {

	public int delete(Term t) throws SQLException {
		return getSqlMapClientTemplate().delete("delete-term",t);
	}

	public List<Term> findAll() throws SQLException {
		return (List<Term>) getSqlMapClientTemplate().queryForList("findall-terms");
	}

	public List<Term> findByCategories(String category) throws SQLException {
		return (List<Term>) getSqlMapClientTemplate().queryForList("find-by-cat-term",category);
	}

	public List<Term> findByWeightThreshold(double threshold) throws SQLException {
		Term t = new Term();
		t.setWeight(threshold);
		return (List<Term>) getSqlMapClientTemplate().queryForList("find-by-weight-term",t);
	}

	public int save(Term t) throws SQLException {
		if (t.getId() == 0) {
			// insert new term
			return ((Term) getSqlMapClientTemplate().insert("insert-term",t)).getId();
		} else {
			return getSqlMapClientTemplate().update("update-term",t);
		}
	}

}
